let submitButton = null;
let passwordForm = null;
let userPassword = null;
let newPassword = null;
let newPasswordCheck = null;

$(function () {
    submitButton = $('#password-Modify-btn');
    passwordForm = $('#passwordForm');
    userPassword = $('#userPassword');
    newPassword = $('#newPassword');
    newPasswordCheck = $('#newPasswordCheck');

    userPassword.on("blur", function () {
        $.ajax({
            type: "POST",
            url: path + "/user/checkPassword",
            data: {userPassword: userPassword.val()},
            dataType: "json",
            success: function (data) {
                if (data.msg === "error") {
                    validateTip(userPassword.next(), {"color": "red"}, imageNo + " 原密码不正确", false);
                } else if (data.msg === "right") {
                    validateTip(userPassword.next(), {"color": "green"}, imageYes + "", true);
                }
            },
            error: function () {
                alert("checkPassword_ajax错误");
            }
        })
    });

    newPassword.on("blur", function () {
        if (userPassword.attr("validateStatus") !== "true") return;
        if (newPassword.val().length < 6 || newPassword.val().length > 20) {
            validateTip(newPassword.next(), {"color": "red"}, imageNo + " 新密码不符合规范", false);
        } else {
            validateTip(newPassword.next(), {"color": "green"}, imageYes + "", true);
        }
    });

    newPasswordCheck.on("blur", function () {
        if (newPassword.attr("validateStatus") !== "true") return;
        if (newPasswordCheck.val() !== newPassword.val()) {
            validateTip(newPasswordCheck.next(), {"color": "red"}, imageNo + " 两次密码不相同", false);
        } else {
            validateTip(newPasswordCheck.next(), {"color": "green"}, imageYes + "", true);
        }
    });

    submitButton.on("click", function () {
        if (userPassword.attr("validateStatus") !== "true") {
            userPassword.blur();
        } else if (newPassword.attr("validateStatus") !== "true") {
            newPassword.blur();
        } else if (newPasswordCheck.attr("validateStatus") !== "true") {
            newPasswordCheck.blur();
        } else if (confirm("确认信息无误了吗?")) {
            passwordForm.submit();
        }
    })
})